package com.xinmiao.test8;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.ImageView;

import com.xinmiao.test8.adaptere2.BaseAdater;
import com.xinmiao.test8.adaptere2.Datas;
import com.xinmiao.test8.adaptere2.ListAdapter;
import com.xinmiao.test8.imagemanager.GlideApp;
import com.xinmiao.test8.imagemanager.ImageLoaderOptions;
import com.xinmiao.test8.imagemanager.ImageManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ListRecvclerActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_recvcler);



        mRecyclerView = findViewById(R.id.imazgereclistview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        List<Datas> list = new ArrayList<>();
        initList(list);
        ListAdapter listadapter = new ListAdapter(list);
        BaseAdater adapter = new BaseAdater(listadapter);
        View headView = getLayoutInflater().inflate(R.layout.itmeheadview, mRecyclerView,false);
        ImageView imageView = headView.findViewById(R.id.headimage);
        ImageManager.getInstance().showImage(
                new ImageLoaderOptions.Builder(imageView, "http://p0.so.qhimgs1.com/bdr/_240_/t019f161c7c1e315d6c.jpg")
                        .setPlaceholder(R.mipmap.ic_launcher)
                        .build());
        adapter.addHeaderView(headView);

        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        new ItemTouchHelper(new ItmeTouch<Datas>(list, adapter)).attachToRecyclerView(mRecyclerView);
        setListener();
    }

    private void setListener() {
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_IDLE:
                        GlideApp.with(ListRecvclerActivity.this).resumeRequests();
                        break;
                    case RecyclerView.SCROLL_STATE_DRAGGING:
                        GlideApp.with(ListRecvclerActivity.this).resumeRequests();
                        break;
                    case RecyclerView.SCROLL_STATE_SETTLING:
                        GlideApp.with(ListRecvclerActivity.this).pauseRequests();
                        break;
                }

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }

    private void initList(List<Datas> list) {
        for (int i = 0; i < 20; i++) {
            Datas data = new Datas();
            data.setContent("第一次发布开源库，如果你有任何建议或问题，" +
                    "请及时联系我。如果你对本控件有优化，欢迎 fork 提 pr。");
            data.setImagHead("http://p1.so.qhmsg.com/t0162c3a318ffcc7266.jpg");
            data.setName("小美女");
            List<String> images = new ArrayList<>();

            int rodom = new Random().nextInt(9);

            for (int j = 0; j < rodom; j++) {
                images.add("http://p1.so.qhimgs1.com/t01cf1f689a30b38311.jpg");
            }

            data.setUrls(images);


            list.add(data);
        }
    }
}
